package cn.hdu.liang.ch5;

import cn.hdu.liang.ch4.Dish;

import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static cn.hdu.liang.ch4.Dish.menu;

public class NumberStream {
    public static void main(String[] args) {
        int sum = menu.stream().mapToInt(Dish::getCalories).sum();
        System.out.println("总热量 : " + sum);

        OptionalInt maxCa = menu.stream().mapToInt(Dish::getCalories).max();
        int max = maxCa.orElse(1);
        System.out.println("最大值 : " + max);

        IntStream evenNums = IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0);
        System.out.println(evenNums.count());

        //所有的勾股数
        Stream<int[]> ggTriples = IntStream.rangeClosed(1, 50).boxed()
                .flatMap(a -> IntStream.rangeClosed(a, 50)
                        .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0).boxed()
                        .map(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)}));
        ggTriples.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));

        Stream<double[]> ggTriples2 = IntStream.rangeClosed(1, 50).boxed()
                .flatMap(a -> IntStream.rangeClosed(a, 50)
                        .mapToObj(b -> new double[]{a, b, Math.sqrt(a * a + b * b)})
                        .filter(t -> t[2] % 1 == 0));
        ggTriples2.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));

    }
}
